<template>
  <div>
    <div>
      <MapContainer/>
      <el-button @click="gg">走</el-button>
    </div>

  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）,
//例如：import 《组件名称》 from '《组件路径》,
import MapContainer from "@/views/amap/MapContainer.vue";
import {getInfo} from "@/api/login";

export default {
  //import引入的组件需要注入到对象中才能使用"
  components: {
    MapContainer
  },
  data() {
    return {
      cacheUserInfo: {},
      count:0,
      arr:[
        [121.697231,30.869863],[121.697755,30.86979],[121.697755,30.86979],[121.697884,30.870616],[121.698013,30.871463],[121.698029,30.871581],[121.698126,30.872225],[121.698249,30.873041],[121.698281,30.873239],[121.698308,30.873384],[121.698308,30.873384],[121.699805,30.873636],[121.700572,30.873743],[121.7018,30.873936],[121.701934,30.873958],[121.701709,30.87532],[121.701586,30.876254],[121.701489,30.877171],[121.701344,30.879065],[121.701307,30.880121],[121.701301,30.880191],[121.70128,30.880931],[121.701258,30.882568],[121.701237,30.88334],[121.701146,30.884687],[121.701055,30.885593],[121.70091,30.886602],[121.700561,30.888436],[121.700363,30.889402],[121.699558,30.893329],[121.69951,30.893554],[121.699327,30.894455],[121.699091,30.895614],[121.698533,30.898339],[121.698077,30.900635],[121.697664,30.90269],[121.697455,30.903779],[121.697423,30.903929],[121.697289,30.90454],[121.697171,30.90505],[121.697031,30.905769],[121.696951,30.906311],[121.696833,30.906917],[121.696253,30.909605],[121.695561,30.912989],[121.695422,30.913751],[121.695406,30.913837],[121.69539,30.913923],[121.695288,30.914395],[121.695116,30.915221],[121.695052,30.915527],[121.694939,30.916085],[121.694515,30.918171],[121.694317,30.919207],[121.694129,30.920172],[121.693968,30.920247],[121.692697,30.919985],[121.692665,30.919722],[121.692831,30.91888],[121.692852,30.918719],[121.692874,30.918611],[121.692874,30.918611],[121.692917,30.918407],[121.692986,30.918225],[121.693061,30.917951],[121.693088,30.917764],[121.693105,30.917576],[121.693099,30.917479],[121.693088,30.917388],[121.693061,30.917286],[121.692992,30.917141],[121.692879,30.916986],[121.692777,30.916884],[121.692713,30.916841],[121.692627,30.916798],[121.692203,30.916605],[121.692096,30.916541],[121.691597,30.916412],[121.689763,30.915961],[121.688164,30.915527],[121.688089,30.915505],[121.686115,30.914915],[121.68457,30.914406],[121.684495,30.914379],[121.682966,30.913869],[121.662597,30.906992],[121.655779,30.904696],[121.655323,30.90454],[121.651766,30.903344],[121.650479,30.902936],[121.649309,30.902631],[121.64689,30.902073],[121.646123,30.901917],[121.645023,30.901724],[121.643494,30.901499],[121.642486,30.90137],[121.640936,30.901225],[121.640625,30.901204],[121.638704,30.901107],[121.637642,30.901086],[121.635984,30.901102],[121.63445,30.90115],[121.633232,30.901247],[121.631655,30.901397],[121.63069,30.90152],[121.629188,30.901751],[121.628291,30.901906],[121.627342,30.902094],[121.626167,30.902352],[121.624606,30.902754],[121.62385,30.902963],[121.622788,30.903285],[121.620363,30.904085],[121.619161,30.904492],[121.619161,30.904492],[121.61899,30.90454],[121.618652,30.904653],[121.616989,30.905206],[121.615546,30.905688],[121.6151,30.905849],[121.613861,30.906236],[121.612333,30.906745],[121.609849,30.907571],[121.608325,30.908102],[121.606533,30.908687],[121.604023,30.909454],[121.602634,30.909803],[121.60081,30.910211],[121.599485,30.910473],[121.597811,30.910747],[121.596679,30.910897],[121.596068,30.910978],[121.594576,30.911128],[121.592827,30.911257],[121.591508,30.91131],[121.590253,30.911343],[121.588627,30.911337],[121.587924,30.911316],[121.577753,30.910763],[121.575876,30.910683],[121.574706,30.910656],[121.574191,30.91064],[121.572904,30.910624],[121.570678,30.910645],[121.56916,30.910666],[121.567293,30.910693],[121.567148,30.910699],[121.56181,30.910785],[121.561397,30.910795],[121.56099,30.910811],[121.559563,30.910806],[121.558136,30.910828],[121.552734,30.910913],[121.538904,30.911133],[121.536506,30.911123],[121.535283,30.911085],[121.534307,30.911037],[121.533288,30.910972],[121.531582,30.910833],[121.530761,30.910768],[121.529484,30.910677],[121.519367,30.909878],[121.516079,30.909626],[121.515204,30.909556],[121.513466,30.909417],[121.512603,30.909347],[121.511304,30.90925],[121.51058,30.909191],[121.508788,30.909052],[121.507125,30.908923],[121.504406,30.908703],[121.503344,30.908601],[121.501203,30.908322],[121.49763,30.907759],[121.493741,30.907115],[121.491805,30.90681],[121.49004,30.906509],[121.489337,30.906396],[121.489337,30.906396],[121.489047,30.906418],[121.488409,30.906396],[121.487781,30.906391],[121.487261,30.90637],[121.487036,30.906343],[121.487036,30.906343],[121.486816,30.906321],[121.48681,30.906321],[121.486081,30.906176],[121.48585,30.906139],[121.485663,30.906134],[121.485325,30.906182],[121.484997,30.906305],[121.484842,30.906396],[121.484627,30.90659],[121.484477,30.906788],[121.484413,30.906901],[121.484343,30.907062],[121.484021,30.90807],[121.483924,30.908263],[121.48386,30.908338],[121.483667,30.908698],[121.483388,30.909191],[121.483109,30.909733],[121.483007,30.909878],[121.481865,30.913166],[121.481403,30.914502],[121.481055,30.915527],[121.47907,30.921245],[121.478115,30.924024],[121.477761,30.925091],[121.477718,30.925226],[121.477718,30.925226],[121.477396,30.926191],[121.477294,30.926513],[121.476613,30.928503],[121.476355,30.929163],[121.476216,30.929506],[121.475738,30.930896],[121.475352,30.932022],[121.474832,30.933455],[121.474424,30.934501],[121.473796,30.935987],[121.47311,30.9375],[121.470385,30.943481],[121.468105,30.948486],[121.464843,30.955642],[121.464135,30.957203],[121.463625,30.958399],[121.463212,30.959429],[121.463196,30.959472],[121.46281,30.960507],[121.461791,30.963393],[121.461254,30.964906],[121.460653,30.966601],[121.45972,30.969246],[121.459286,30.970458],[121.458856,30.971676],[121.458717,30.972084],[121.458615,30.972363],[121.455911,30.979991],[121.455434,30.981246],[121.455353,30.981445],[121.454624,30.983156],[121.45398,30.98454],[121.453181,30.986101],[121.449935,30.99205],[121.449726,30.992431],[121.44845,30.99477],[121.447645,30.996138],[121.447591,30.996224],[121.447092,30.997066],[121.446572,30.997935],[121.4464,30.998241],[121.443573,31.003417],[121.443026,31.004415],[121.44287,31.0047],[121.442114,31.006089],[121.442018,31.00626],[121.441148,31.007907],[121.440988,31.008176],[121.440762,31.008551],[121.437613,31.014313],[121.437565,31.014404],[121.436508,31.016335],[121.436079,31.017172],[121.435736,31.017869],[121.435301,31.018792],[121.434325,31.021002],[121.433976,31.021892],[121.433579,31.02303],[121.433273,31.023791],[121.432898,31.024639],[121.43256,31.02539],[121.43233,31.025916],[121.432281,31.026034],[121.432228,31.026157],[121.432228,31.026157],[121.432034,31.026576],[121.431846,31.026994],[121.431723,31.027268],[121.430951,31.029022],[121.429191,31.032916],[121.42858,31.034273],[121.427614,31.036376],[121.427555,31.036505],[121.427351,31.037015],[121.427351,31.037015],[121.427142,31.037669],[121.427088,31.037905],[121.426879,31.038345],[121.426413,31.039407],[121.426413,31.039407],[121.426402,31.039557],[121.426343,31.039783],[121.426284,31.040126],[121.426273,31.040491],[121.426305,31.040797],[121.426353,31.041006],[121.42653,31.041558],[121.42653,31.041558],[121.426622,31.041853],[121.426643,31.042068],[121.426643,31.042159],[121.426611,31.042352],[121.426552,31.042545],[121.426445,31.042755],[121.426348,31.042894],[121.426219,31.043028],[121.426139,31.043103],[121.425924,31.043243],[121.425661,31.043361],[121.425474,31.04342],[121.425259,31.043452],[121.425055,31.043463],[121.424825,31.043447],[121.424674,31.04342],[121.424476,31.043366],[121.424251,31.04327],[121.424116,31.043189],[121.423929,31.043039],[121.42372,31.042814],[121.423387,31.042347],[121.423237,31.042175],[121.423086,31.042046],[121.422952,31.041961],[121.42277,31.04187],[121.422142,31.041644],[121.421155,31.041322],[121.420898,31.041236],[121.420592,31.041135],[121.420061,31.040952],[121.419203,31.040614],[121.419085,31.04055],[121.418961,31.040453],[121.418602,31.040308],[121.417974,31.040099],[121.416982,31.039772],[121.412663,31.038324],[121.407932,31.036752],[121.406875,31.03643],[121.406671,31.036376],[121.405861,31.036167],[121.40488,31.035931],[121.403866,31.035711],[121.40319,31.035593],[121.400491,31.035169],[121.40032,31.035143],[121.400025,31.0351],[121.399617,31.035051],[121.399515,31.035035],[121.399515,31.035035],[121.399456,31.035041],[121.399113,31.035057],[121.398925,31.035041],[121.397831,31.03496],[121.397557,31.03496],[121.397246,31.034971],[121.396935,31.035008],[121.396651,31.035062],[121.396329,31.035153],[121.396039,31.035255],[121.395776,31.035368],[121.395556,31.035486],[121.39531,31.035636],[121.395063,31.035818],[121.394467,31.036301],[121.394398,31.036376],[121.39428,31.036505],[121.394092,31.036709],[121.393571,31.037278],[121.392718,31.038329],[121.392718,31.038329],[121.391887,31.039236],[121.391887,31.039236],[121.391769,31.039407],[121.391619,31.039697],[121.391511,31.039949],[121.391195,31.040909],[121.390632,31.042669],[121.390256,31.043506],[121.390106,31.043774],[121.389859,31.044155],[121.388121,31.04652],[121.387826,31.046939],[121.387617,31.047271],[121.387563,31.047363],[121.387284,31.047845],[121.386566,31.049321],[121.386501,31.049455],[121.386201,31.050125],[121.385734,31.051198],[121.384865,31.053349],[121.384624,31.053918],[121.384173,31.05491],[121.383856,31.05556],[121.382408,31.058349],[121.380295,31.062415],[121.379844,31.063284],[121.379619,31.063713],[121.379275,31.064373],[121.377591,31.067576],[121.37699,31.068729],[121.376953,31.068799],[121.37676,31.069158],[121.37676,31.069158],[121.376668,31.069335],[121.376019,31.070596],[121.375461,31.071765],[121.374217,31.074743],[121.373535,31.076395],[121.37265,31.07853],[121.372452,31.079013],[121.371926,31.080284],[121.37191,31.080322],[121.371459,31.081395],[121.371003,31.082425],[121.370596,31.083293],[121.368364,31.087805],[121.367533,31.089532],[121.367114,31.090466],[121.366755,31.091308],[121.366637,31.091592],[121.366229,31.092579],[121.366122,31.092837],[121.365907,31.093357],[121.365682,31.09391],[121.364625,31.096511],[121.364432,31.097005],[121.364196,31.097649],[121.363944,31.098432],[121.363708,31.099194],[121.36337,31.100454],[121.363257,31.100819],[121.363118,31.101189],[121.362957,31.10157],[121.362603,31.102294],[121.362474,31.102552],[121.362066,31.103319],[121.361058,31.105293]
      ]
    }
  },
  //计算属性 类似于data概念",
  computed: {},
  //监控data中的数据变化",
  watch: {},
  //方法集合",
  methods: {
    gg(){
        setTimeout(()=>{
          MapContainer.methods.updateTrack(this.arr[this.count++])
          this.gg()
        },500)
    },
    info() {
      getInfo().then(res => {
        if (res.code == 200) {
          this.cacheUserInfo.userId = res.data.user.userId;
          this.cacheUserInfo.deptId = res.data.user.deptId;
          this.cacheUserInfo.userName = res.data.user.userName;
          this.cacheUserInfo.nickName = res.data.user.nickName;
          this.cacheUserInfo.userType = res.data.user.userType;
          this.cacheUserInfo.avatar = res.data.user.avatar;
          this.cacheUserInfo.password = res.data.user.password;
          this.cacheUserInfo.status = res.data.user.status;
        }
      })
    },
    initWebSocket() {
      if (!window.WebSocket) {
        window.WebSocket = window.MozWebSocket;
      }

      if (window.WebSocket) {
        this.socket = new WebSocket("ws://127.0.0.1:9331");
        this.socket.onmessage = this.handleWebSocketMessage;
        this.socket.onclose = this.handleWebSocketClose;

        // 在socket连接打开后发送心跳请求
        this.socket.onopen = () => {
          console.log("WebSocket已成功连接！");

          if (this.socket.readyState === WebSocket.CONNECTING) {
          } else if (this.socket.readyState === WebSocket.OPEN) {
            this.setupHeartbeat();
            this.sendUserData();
          } else {
            console.log('WebSocket连接没有开启！');
            clearInterval(this.uuid);
            this.$message({
              message: 'Websocket连接没有开启！',
              type: 'warning'
            });
          }
        };

        // socket关闭
        this.socket.onclose = () => {
          console.log('WebSocket已关闭！');
          clearInterval(this.uuid);
        };
      } else {
        this.$message({
          message: '您的浏览器不支持WebSocket！',
          type: 'warning'
        });
      }
    },

    sendUserData() {
      let data = {
        type: 7,
        params: this.cacheUserInfo,
      };

      this.socket.send(JSON.stringify(data));
    },
    //传送经纬度
    sendPositioning(message) {
      this.socket.send(JSON.stringify(message));
    },
    setupHeartbeat() {
      clearInterval(this.uuid);
      this.uuid = setInterval(() => {
        if (this.socket.readyState === WebSocket.OPEN) {
          let heartbeatData = {
            type: 11,
            params: {},
          };
          this.socket.send(JSON.stringify(heartbeatData));
        } else {
          console.log('Websocket连接没有开启！');
          clearInterval(this.uuid);
        }
      }, 1000 * 25);
    },
    handleWebSocketMessage(event) {
      let json = JSON.parse(event.data);

      if (json.status == 200) {
        switch (json.type) {
          case 2:
            this.handleSingleMessage(json);
            break;
          case 4:
            this.handleCreateChatResponse(json);
            break;
          case 10:
            this.handleGroupMessageResponse(json);
            break;
          case 12:
            console.log("收到心跳检测回复");
            break;
          default:
            break;
        }
      } else {
        this.$message({
          message: '发送失败 对方不在线',
          type: 'warning'
        });
      }
    },
    handleWebSocketClose(event) {
      console.log('WebSocket已关闭！');
    },

  },
  //生命周期 - 创建完成（可以访问当前this实例）",
  created() {
    this.initWebSocket();
    this.info()
  },
  //生命周期 - 挂载完成（可以访问DOM元素）",
  mounted() {
  },
  beforeCreate() {
  }, //生命周期 - 创建之前",
  beforeMount() {
  }, //生命周期 - 挂载之前",
  beforeUpdate() {
  }, //生命周期 - 更新之前",
  updated() {
  }, //生命周期 - 更新之后",
  beforeDestroy() {
  }, //生命周期 - 销毁之前",
  destroyed() {
  }, //生命周期 - 销毁完成",
  activated() {
  } //如果页面有keep-alive缓存功能，这个函数会触发",
};
</script>
<style scoped>

</style>
